package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Autowired
    AdSensitiveMapper adSensitiveMapper;


    @Override
    public ResponseResult findByNameAndPage(SensitiveDto sensitiveDto) {
        if (sensitiveDto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitiveDto.checkParam();
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            lambdaQueryWrapper.like(AdSensitive::getSensitives, sensitiveDto.getName());
        }
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        IPage<AdSensitive> adSensitiveResult = this.page(page, lambdaQueryWrapper);
        if (adSensitiveResult == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        PageResponseResult pageResponseResult =
                new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) adSensitiveResult.getTotal());
        pageResponseResult.setData(adSensitiveResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数校验
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.参数填充
        adSensitive.setCreatedTime(new Date());
        //3.进行添加
        boolean saveResult = this.save(adSensitive);
        if (!saveResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FAIL);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {

        //1.参数校验
        if (adSensitive==null || adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.填充参数 无
        //3.进行修改
        boolean updateResult = this.updateById(adSensitive);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        boolean delResult = this.removeById(id);
        if (!delResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAIL);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<String> findAllSenstives() {
        return  list().stream().map(AdSensitive::getSensitives).collect(Collectors.toList());


    }
}
